Ejemplo n.º 1
0
 private void Send(PyPacket packet)
 {
     Send(packet.Encode());
 }
Ejemplo n.º 2
0
        public static void HandlePacket(object input)
        {
            PyPacket packet = (PyPacket)input;
            PyPacket res = new PyPacket();

            if (packet.type == Macho.MachoNetMsg_Type.CALL_REQ)
            {
                PyTuple callInfo = packet.payload.As<PyTuple>().Items[0].As<PyTuple>()[1].As<PySubStream>().Data.As<PyTuple>();

                string call = callInfo.Items[1].As<PyString>().Value;
                PyTuple args = callInfo.Items[2].As<PyTuple>();
                PyDict sub = callInfo.Items[3].As<PyDict>();

                PyObject callRes = null;

                try
                {
                    callRes = Program.SvcMgr.ServiceCall(packet.dest.service, call, args, null);
                }
                catch (ServiceDoesNotContainCallException)
                {
                    Log.Error("HandlePacket", "The service " + packet.dest.service + " does not contain a definition for " + call);
                }
                catch (ServiceDoesNotExistsException)
                {
                    Log.Error("HandlePacket", "The requested service(" + packet.dest.service + ")does not exists");
                }
                catch (Exception ex)
                {
                    Log.Error("HandlePacket", "Unhadled exception: " + ex.ToString());
                }

                if (callRes == null)
                {
                    return;
                }

                if (callRes.Type == PyObjectType.Tuple)
                {
                    res.type_string = "macho.CallRsp";
                    res.type = Macho.MachoNetMsg_Type.CALL_RSP;

                    res.source = packet.dest;

                    res.dest = packet.source;
                    /*
                    res.dest.type = PyAddress.AddrType.Client;
                    res.dest.typeID = (ulong)clients[packet.userID].AccountID;
                    res.dest.callID = packet.source.callID;
                    */

                    res.userID = packet.userID;

                    res.payload = new PyTuple();
                    res.payload.Items.Add(new PySubStream(callRes));

                    Send(res.Encode());
                }
            }
            else if (packet.type == Macho.MachoNetMsg_Type.SESSIONCHANGENOTIFICATION)
            {
                Log.Debug("Main", "Updating session for client " + packet.userID);

                // Check if the client isnt assigned to this node yet
                if(clients.ContainsKey(packet.userID) == false)
                {
                    Client cli = new Client();
                    clients.Add(packet.userID, cli);
                }

                // Update client session
                clients[packet.userID].UpdateSession(packet);
            }
        }
 private void SendClient(PyPacket data)
 {
     SendClient(data.Encode());
 }
 private void SendServer(PyPacket data)
 {
     SendServer(data.Encode());
 }
        private PyObject HandlePyPacket(PyPacket data)
        {
            // Just forward it if we dont want to look for a specific one
            if (data.dest.type == PyAddress.AddrType.Client)
            {
                // Look for SessionChangeNotifications
                if (data.type == Macho.MachoNetMsg_Type.SESSIONCHANGENOTIFICATION)
                {
                    // Search for address change in session
                    PyTuple payload = data.payload;
                    PyDict session = payload.Items[0].As<PyTuple>().Items[1].As<PyDict>();

                    if (session.Contains("address") == true)
                    {
                        PyTuple address = session.Get("address").As<PyTuple>();

                        address[0] = new PyString(socket.GetAddress());
                        address[1] = new PyString(socket.GetAddress());
                        session.Set("address", address);
                    }

                    payload.Items[0].As<PyTuple>().Items[1] = session;

                    data.payload = payload;
                }

                // SendClient(data);
            }
            else if (data.dest.type == PyAddress.AddrType.Node)
            {
                // SendServer(data);
            }
            else if (data.dest.type == PyAddress.AddrType.Broadcast)
            {
                // What to do now ?
                Log.Error("Client", "Broadcast packets not supported yet");
                // throw new NotImplementedException("Broadcast packets are not supported yet");
            }

            return data.Encode();
        }
Ejemplo n.º 6
0
        private void Run()
        {
            try
            {
                while (true)
                {
                    Thread.Sleep(1);

                    byte[] data = new byte[socket.Available];
                    int bytes = 0;

                    try
                    {
                        bytes = socket.Recv(data);
                    }
                    catch (SocketException ex)
                    {
                        if (ex.ErrorCode != 10035)
                            throw new DisconnectException();
                    }
                    catch (Exception)
                    {
                        throw new DisconnectException();
                    }

                    if (bytes == -1) // Client disconnected
                    {
                        throw new DisconnectException();
                    }
                    else if (bytes > 0)
                    {
                        packetizer.QueuePackets(data, bytes);
                        int p = packetizer.ProcessPackets();

                        byte[] actual = null;

                        for (int i = 0; i < p; i++)
                        {
                            actual = packetizer.PopItem();
                            PyObject obj = Unmarshal.Process<PyObject>(actual);

                            if (obj is PyObjectEx)
                            {
                                // PyException
                                Log.Error("Client", "Got exception from client");
                            }
                            else
                            {

                                PyPacket packet = new PyPacket();
                                if (packet.Decode(obj) == false)
                                {
                                    Log.Error("Client", "Error decoding PyPacket");
                                }
                                else
                                {
                                    // Get the node ID to send
                                    if (packet.dest.type == PyAddress.AddrType.Node)
                                    {
                                        if (packet.dest.typeID == 1)
                                        {
                                            packet.dest.typeID = (ulong)nodeID; // We dont want to receive packets in the proxy
                                        }

                                        if (packet.source.type != PyAddress.AddrType.Client)
                                        {
                                            Log.Error("Client", string.Format("Wrong source data, expected client but got {0}", packet.source.type));
                                        }

                                        Log.Warning("Client", PrettyPrinter.Print(packet.Encode()));

                                        if (NodeManager.NotifyNode((int)packet.dest.typeID, obj) == false)
                                        {
                                            // We cant send the data to the node, what to do?
                                            Log.Error("Client", "Trying to send a packet to a non-existing node");
                                            throw new DisconnectException();
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {

            }
            catch (DisconnectException)
            {

            }
            catch (Exception ex)
            {
                Log.Error("Client", "Unhandled exception... " + ex.Message);
                Log.Error("ExceptionHandler", "Stack trace: " + ex.StackTrace);
            }

            // We should notify our node about this
            Log.Error("Client", "Client disconnected");
            socket.Close();
            ClientManager.RemoveClient(this);
        }
Ejemplo n.º 7
0
        public PyObject SetSessionChangeDestination(PyPacket p, int node)
        {
            // The session change info should never come from the client
            p.source.type = PyAddress.AddrType.Node;
            p.source.typeID = (ulong)1;
            p.source.callID = 0;

            p.dest.type = PyAddress.AddrType.Node;
            p.dest.typeID = (ulong)node;
            p.dest.callID = 0;

            return p.Encode();
        }
Ejemplo n.º 8
0
        public PyObject SetSessionChangeDestination(PyPacket p)
        {
            p.source.type = PyAddress.AddrType.Node;
            p.source.typeID = (ulong)nodeID;
            p.source.callID = 0;

            p.dest.type = PyAddress.AddrType.Client;
            p.dest.typeID = (ulong)ClientManager.GetClientID(this);
            p.dest.callID = 0;

            return p.Encode();
        }
Ejemplo n.º 9
0
        public static void HandlePacket(object input)
        {
            PyPacket packet = (PyPacket)input;
            PyPacket res = new PyPacket();

            if (packet.type == Macho.MachoNetMsg_Type.CALL_REQ)
            {
                PyTuple callInfo = packet.payload.As<PyTuple>().Items[0].As<PyTuple>()[1].As<PySubStream>().Data.As<PyTuple>();

                string call = callInfo.Items[1].As<PyString>().Value;
                PyTuple args = callInfo.Items[2].As<PyTuple>();
                PyDict sub = callInfo.Items[3].As<PyDict>();

                if (Program.SvcMgr.FindService(packet.dest.service) == false)
                {
                    Log.Error("Client", "Cannot find service " + packet.dest.service + " to call " + call);
                    return;
                }

                if (Program.SvcMgr.GetService(packet.dest.service).FindServiceCall(call) == false)
                {
                    Log.Error("Client", "Service " + packet.dest.service + " doesnt contains a call to " + call);
                    return;
                }

                PyObject callRes = Program.SvcMgr.Call(packet.dest.service, call, args, null);

                if (callRes == null)
                {
                    return;
                }

                if (callRes.Type == PyObjectType.Tuple)
                {
                    res.type_string = "macho.CallRsp";
                    res.type = Macho.MachoNetMsg_Type.CALL_RSP;

                    res.source = packet.dest;

                    res.dest.type = PyAddress.AddrType.Client;
                    res.dest.typeID = (ulong)clients[packet.userID].GetAccountID();
                    res.dest.callID = packet.source.callID;

                    res.userID = (uint)clients[packet.userID].GetAccountID();

                    res.payload = new PyTuple();
                    res.payload.Items.Add(new PySubStream(callRes));

                    Send(res.Encode());
                }
            }
            else if (packet.type == Macho.MachoNetMsg_Type.SESSIONCHANGENOTIFICATION)
            {
                Log.Debug("Main", "Updating session for client " + packet.userID);

                // Check if the client isnt assigned to this node yet
                if(clients.ContainsKey(packet.userID) == false)
                {
                    Client cli = new Client();
                    clients.Add(packet.userID, cli);
                }

                // Update client session
                clients[packet.userID].UpdateSession(packet);
            }
        }