public void SendSessionChange() { PyPacket sc = CreateEmptySessionChange(); PyObject client = SetSessionChangeDestination(sc); PyObject node = SetSessionChangeDestination(sc, nodeID); if (sc != null) { Send(client); NodeManager.NotifyNode(nodeID, node); } }
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); }
public void Run() { while (true) { Thread.Sleep(1); try { byte[] data = new byte[socket.Available]; int bytes = socket.Recv(data); if (bytes == -1) { throw new DisconnectException(); } else if (bytes > 0) { packetizer.QueuePackets(data, bytes); int p = packetizer.ProcessPackets(); for (int i = 0; i < p; i++) { byte[] packet = packetizer.PopItem(); PyObject obj = Unmarshal.Process <PyObject>(packet); if (obj.Type == PyObjectType.ObjectData) { Log.Warning("Node", PrettyPrinter.Print(obj)); PyObjectData item = obj as PyObjectData; if (item.Name == "macho.CallRsp") { PyPacket final = new PyPacket(); if (final.Decode(item) == true) { if (final.dest.type == PyAddress.AddrType.Client) { try { ClientManager.NotifyClient((int)final.userID, obj); } catch (Exception) { Log.Error("Node", "Trying to send a packet to a non-existing client"); } } else if (final.dest.type == PyAddress.AddrType.Node) { NodeManager.NotifyNode((int)final.dest.typeID, obj); } else if (final.dest.type == PyAddress.AddrType.Broadcast) { // This should not be coded like this here, but will do the trick for now // TODO: Add a ClientManager ClientManager.NotifyClients(obj); } } } } else { Log.Error("Node", "Unknown type"); } } } } catch (SocketException ex) { if (ex.ErrorCode != 10035) { break; } } catch (DisconnectException) { Log.Error("Node", "Node " + NodeManager.GetNodeID(this) + " disconnected"); break; } catch (Exception) { } } }