public Client(Socket sock) { socket = sock; AsyncState state = new AsyncState(); // Start receiving socket.BeginReceive(state.buffer, 0, 8192, SocketFlags.None, recvAsync, state); }
public void ReceiveAsync(IAsyncResult ar) { AsyncState state = (AsyncState)(ar.AsyncState); int bytes = socket.EndReceive(ar); packetizer.QueuePackets(state.buffer, bytes); int p = packetizer.ProcessPackets(); socket.BeginReceive(state.buffer, 0, 8192, SocketFlags.None, recvAsync, state); }
static void AcceptAsync(IAsyncResult ar) { TCPSocket handler = (TCPSocket)(ar.AsyncState); Socket sock = handler.Socket.EndAccept(ar); Log.Debug("Cluster", "Incoming connection"); AsyncState state = new AsyncState(); handler.Socket.BeginAccept(acceptAsync, handler); ConnectionManager.AddConnection(sock); }
static void AcceptAsync(IAsyncResult ar) { TCPSocket handler = (TCPSocket)(ar.AsyncState); Socket sock = handler.Socket.EndAccept(ar); AsyncState state = new AsyncState(); // sock.BeginReceive(state.buffer, 0, 8192, SocketFlags.None, recvAsync, state); handler.Socket.BeginAccept(acceptAsync, handler); ClientManager.AddClient(sock); }
public Connection(Socket sock) { Socket = new TCPSocket(sock, false); // Declare handlers recvAsync = new AsyncCallback(ReceiveAuthAsync); sendAsync = new AsyncCallback(SendAsync); // Session data Session = new Session(); StageEnded = false; // Send LowLevel version exchange SendLowLevelVersionExchange(); AsyncState state = new AsyncState(); // Start receiving Socket.Socket.BeginReceive(state.buffer, 0, 8192, SocketFlags.None, recvAsync, state); }
public void ReceiveAuthAsync(IAsyncResult ar) { try { AsyncState state = (AsyncState)(ar.AsyncState); int bytes = Socket.Socket.EndReceive(ar); packetizer.QueuePackets(state.buffer, bytes); int p = packetizer.ProcessPackets(); for (int i = 0; i < p; i++) { try { byte[] packet = packetizer.PopItem(); PyObject obj = Unmarshal.Process <PyObject>(packet); if (obj != null) { PyObject result = TCPHandler.ProcessAuth(obj, this); if (result != null) { Send(result); } if (StageEnded == true) { if (Type == ConnectionType.Node) { recvAsync = new AsyncCallback(ReceiveNodeAsync); } else if (Type == ConnectionType.Client) { recvAsync = new AsyncCallback(ReceiveClientAsync); } // Exit from the loop to keep the packets in the list ;) break; } } } catch (Exception ex) { Log.Error("Connection", ex.ToString()); } } // Continue receiving data Socket.Socket.BeginReceive(state.buffer, 0, 8192, SocketFlags.None, recvAsync, state); } catch (ObjectDisposedException) { Log.Debug("Connection", "Disconnected"); ConnectionManager.RemoveConnection(this); } catch (SocketException) { Log.Debug("Connection", "Disconnected"); ConnectionManager.RemoveConnection(this); } catch (Exception ex) { Log.Error("Connection", "Caught unhandled exception: " + ex.ToString()); } }
public void ReceiveClientAsync(IAsyncResult ar) { try { AsyncState state = (AsyncState)(ar.AsyncState); int bytes = Socket.Socket.EndReceive(ar); packetizer.QueuePackets(state.buffer, bytes); int p = packetizer.ProcessPackets(); for (int i = 0; i < p; i++) { byte[] actual = packetizer.PopItem(); PyObject obj = Unmarshal.Process <PyObject>(actual); if (obj == null) { continue; } 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 { if (packet.dest.type == PyAddress.AddrType.Node) { if (packet.source.type != PyAddress.AddrType.Client) { Log.Error("Client", string.Format("Wrong source data, expected client but got {0}", packet.source.type)); } // Notify the node, be careful here, the client will be able to send packets to game clients if (packet.dest.typeID == 0xFFAA) { Log.Warning("Client", "Sending packet to proxy"); ConnectionManager.NotifyNode((int)(packet.dest.typeID), obj); } else { ConnectionManager.NotifyNode((int)(packet.dest.typeID), obj); } } } } } Socket.Socket.BeginReceive(state.buffer, 0, 8192, SocketFlags.None, recvAsync, state); } catch (ObjectDisposedException) { Log.Debug("Client", "Disconnected"); ConnectionManager.RemoveConnection(this); } catch (SocketException) { Log.Debug("Client", "Disconnected"); ConnectionManager.RemoveConnection(this); } catch (Exception ex) { Log.Error("Client", "Caught unhandled exception: " + ex.ToString()); } }
public void ReceiveNodeAsync(IAsyncResult ar) { try { AsyncState state = (AsyncState)(ar.AsyncState); int bytes = Socket.Socket.EndReceive(ar); packetizer.QueuePackets(state.buffer, bytes); int p = packetizer.ProcessPackets(); for (int i = 0; i < p; i++) { byte[] packet = packetizer.PopItem(); PyObject obj = Unmarshal.Process <PyObject>(packet); if (obj == null) { Log.Debug("Node", "Null packet received"); continue; } if ((obj is PyObjectData) == false) { Log.Debug("Node", "Non-valid node packet. Dropping"); continue; } PyObjectData item = obj as PyObjectData; if (item.Name == "macho.CallRsp") { PyPacket final = new PyPacket(); if (final.Decode(item) == false) { Log.Error("Node", "Cannot decode packet"); continue; } if (final.dest.type == PyAddress.AddrType.Client) { ConnectionManager.NotifyClient((int)(final.userID), obj); } else if (final.dest.type == PyAddress.AddrType.Node) { ConnectionManager.NotifyNode((int)(final.dest.typeID), obj); } else if (final.dest.type == PyAddress.AddrType.Broadcast) { Log.Error("Node", "Broadcast packets not supported yet"); } // TODO: Handle Broadcast packets } else { Log.Error("Node", string.Format("Wrong packet name: {0}", item.Name)); } } Socket.Socket.BeginReceive(state.buffer, 0, 8192, SocketFlags.None, recvAsync, state); } catch (ObjectDisposedException) { Log.Debug("Node", "Disconnected"); ConnectionManager.RemoveConnection(this); } catch (SocketException) { Log.Debug("Node", "Disconnected"); ConnectionManager.RemoveConnection(this); } catch (Exception ex) { Log.Error("Node", "Caught unhandled exception: " + ex.ToString()); } }