Beispiel #1
0
def getDataMessageWithBlob(blob):
    imsg = c2s_pb2.Data()
    imsg.errcode = c2s_pb2.Data.ERROR_NOERROR
    imsg.blob = blob
    msg = metaMessage_pb2.Wrapper()
    msg.Data.MergeFrom(imsg)
    return msg
Beispiel #2
0
def getSessionMessage(code_tuple):
    imsg = c2s_pb2.Session()
    imsg.opcode = code_tuple[0]
    imsg.errcode = code_tuple[1]
    msg = metaMessage_pb2.Wrapper()
    msg.Session.MergeFrom(imsg)
    return msg
Beispiel #3
0
def getSessionMessageWithSecret(secret):
    imsg = c2s_pb2.Session()
    imsg.opcode = c2s_pb2.Session.SESSION_CREATE_SUCCESS
    imsg.errcode = c2s_pb2.Session.ERROR_NOERROR
    imsg.session_secret = secret
    msg = metaMessage_pb2.Wrapper()
    msg.Session.MergeFrom(imsg)
    return msg
Beispiel #4
0
def RecvOneMsg(sock):
    lengthbuf = SocketReadN(sock, 4)
    length = struct.unpack('>i', lengthbuf)[0]
    wrapper = metaMessage_pb2.Wrapper()
    wrapper.ParseFromString(SocketReadN(sock, length))
    return wrapper
Beispiel #5
0
def getDataMessage(errcode):
    imsg = c2s_pb2.Data()
    imsg.errcode = errcode
    msg = metaMessage_pb2.Wrapper()
    msg.Data.MergeFrom(imsg)
    return msg
Beispiel #6
0
    if peer is not None:
        try:
            # print "Forwarding message"
            sendMessage(message, peer)
            # print "Notifying sender"
            errcode = c2s_pb2.Data.ERROR_NOERROR
        except Exception, e:
            print "Error while trying to forward data message: ", e
            NotifySessionStatus(c2s_pb2.Session.SESSION_PEER_LEFT, sock)
            del PAIRS[sock]
            del PAIRS[peer]
            LIMBO.append(sock)
            errcode = c2s_pb2.Data.ERROR_TRANSMISSION_FAILED
    else:
        errcode = c2s_pb2.Data.ERROR_NO_SESSION
    wrapper = metaMessage_pb2.Wrapper()
    wrapper.ParseFromString(message.Data.blob)
    prettyPrintProtobuf(wrapper, sock)
    return getDataMessage(errcode)


def HandleSessionMessage(message, sock):
    if message.opcode == c2s_pb2.Session.SESSION_CREATE:
        secret = NewSession(sock)
        if secret != "":
            return getSessionMessageWithSecret(secret)
        else:
            return getSessionMessage(
                (c2s_pb2.Session.SESSION_CREATE_FAIL,
                 c2s_pb2.Session.ERROR_CREATE_ALREADY_HAS_SESSION))
    elif message.opcode == c2s_pb2.Session.SESSION_JOIN: