void AcceptCallback(IAsyncResult ar) { allDone.Set(); Socket listener = (Socket)ar.AsyncState; Socket handler = null; try { handler = listener.EndAccept(ar); } catch (Exception e) { if (handler != null) { handler.Dispose(); } return; } if (ConnectedCount >= Constants.MAXCLIENTS) { Debug.WriteLine("Too man clients disconnecting new client "); handler.Close(); return; } handler.NoDelay = true; handler.Blocking = false; var state = new ServerSocket(); state.SocketObject = handler; // LOG += "Accept Pending Connection<br/>"; Debug.WriteLine("Accept Pending Connection"); lock (PendingConnectionsLock) { PendingConnections.Add(state); } }
public List <Client_Wrapper> Add(ServerSocket c, int dst_id, int src_id) { //BEGING VALIDATION Debug.WriteLine("Attempting to pair connections dst_id " + dst_id.ToString() + "src_id " + src_id); var newclient = new Client_Wrapper(); newclient.SocketObject = c; newclient.ClientObject.ConnectTime = DateTime.Now; if (dst_id == -1) { newclient.ClientObject.Host = Client.Host_Type.Server; } else if (src_id == -1) { newclient.ClientObject.Host = Client.Host_Type.Viewer; //servers have their info set when they get an ID, viewers dont until they are added var remoteIpEndPoint = newclient.SocketObject.SocketObject.RemoteEndPoint as System.Net.IPEndPoint; newclient.ClientObject.Firewall_IP = remoteIpEndPoint.Address.ToString(); newclient.ClientObject.Firewall_Port = remoteIpEndPoint.Port.ToString(); } else { c.ShouldDisconnect = true; Debug.WriteLine("Disconnecting connection because of unknown client type"); return(null); } //attempt to pair up! //check id range if (newclient.ClientObject.Host == Client.Host_Type.Viewer && (dst_id < 0 || dst_id >= Constants.MAXCLIENTS)) { c.ShouldDisconnect = true; Debug.WriteLine("Disconnecting connection because a viewer cannot connect to a value less than 0 Or greater than " + Constants.MAXCLIENTS.ToString()); return(null); } if (newclient.ClientObject.Host == Client.Host_Type.Server && (src_id < 0 || dst_id >= Constants.MAXCLIENTS)) { c.ShouldDisconnect = true; Debug.WriteLine("Disconnecting connection because a server cannot have a src_id to a value less than 0Or greater than " + Constants.MAXCLIENTS.ToString()); return(null); } //ids are going to be corrected below newclient.ClientObject.Dst_ID = dst_id; newclient.ClientObject.Src_ID = src_id; List <Client_Wrapper> ret = null; //check if ids are already in use //by now the range of the IDs is good and the code and goto the next step if (newclient.ClientObject.Host == Client.Host_Type.Viewer) { ret = AddViewer(newclient); } else if (newclient.ClientObject.Host == Client.Host_Type.Server) { ret = AddServer(newclient); } else { Debug.Assert(false); } if (ret != null) {//either a pairing occured or a client connected if (ret.Count == 1) { if (OnClientConnectEvent != null) { OnClientConnectEvent(ret.Select(a => a.ClientObject).ToList()); } } else if (ret.Count == 2) { if (OnPairedEvent != null) { OnClientConnectEvent(ret.Select(a => a.ClientObject).ToList()); } } else { Debug.Assert(false);//never hit this!! } } return(ret); }
public void PendingClientDisconnecting(ServerSocket s) { }
void AcceptCallback(IAsyncResult ar) { allDone.Set(); Socket listener = (Socket)ar.AsyncState; Socket handler = null; try { handler = listener.EndAccept(ar); } catch (Exception e) { if (handler != null) handler.Dispose(); return; } if (ConnectedCount >= Constants.MAXCLIENTS) { Debug.WriteLine("Too man clients disconnecting new client "); handler.Close(); return; } handler.NoDelay = true; handler.Blocking = false; var state = new ServerSocket(); state.SocketObject = handler; // LOG += "Accept Pending Connection<br/>"; Debug.WriteLine("Accept Pending Connection"); lock (PendingConnectionsLock) { PendingConnections.Add(state); } }
List<Client_Wrapper> PairUp(ServerSocket state) { if (state.BufferCount >= Constants.GATEWAY_HEADER_SIZE) {//if there is enough data, try to pair up //the ids are validated in the manager class return ClientManager.Add(state, BitConverter.ToInt32(state.buffer, 0), BitConverter.ToInt32(state.buffer, 4)); } return null; }
public List<Client_Wrapper> Add(ServerSocket c, int dst_id, int src_id) { //BEGING VALIDATION Debug.WriteLine("Attempting to pair connections dst_id " + dst_id.ToString() + "src_id " + src_id); var newclient = new Client_Wrapper(); newclient.SocketObject = c; newclient.ClientObject.ConnectTime = DateTime.Now; if(dst_id == -1) newclient.ClientObject.Host = Client.Host_Type.Server; else if(src_id == -1) { newclient.ClientObject.Host = Client.Host_Type.Viewer; //servers have their info set when they get an ID, viewers dont until they are added var remoteIpEndPoint = newclient.SocketObject.SocketObject.RemoteEndPoint as System.Net.IPEndPoint; newclient.ClientObject.Firewall_IP = remoteIpEndPoint.Address.ToString(); newclient.ClientObject.Firewall_Port = remoteIpEndPoint.Port.ToString(); } else { c.ShouldDisconnect = true; Debug.WriteLine("Disconnecting connection because of unknown client type"); return null; } //attempt to pair up! //check id range if(newclient.ClientObject.Host == Client.Host_Type.Viewer && (dst_id < 0 || dst_id >= Constants.MAXCLIENTS)) { c.ShouldDisconnect = true; Debug.WriteLine("Disconnecting connection because a viewer cannot connect to a value less than 0 Or greater than " + Constants.MAXCLIENTS.ToString()); return null; } if(newclient.ClientObject.Host == Client.Host_Type.Server && (src_id < 0 || dst_id >= Constants.MAXCLIENTS)) { c.ShouldDisconnect = true; Debug.WriteLine("Disconnecting connection because a server cannot have a src_id to a value less than 0Or greater than " + Constants.MAXCLIENTS.ToString()); return null; } //ids are going to be corrected below newclient.ClientObject.Dst_ID = dst_id; newclient.ClientObject.Src_ID = src_id; List<Client_Wrapper> ret = null; //check if ids are already in use //by now the range of the IDs is good and the code and goto the next step if(newclient.ClientObject.Host == Client.Host_Type.Viewer) { ret = AddViewer(newclient); } else if(newclient.ClientObject.Host == Client.Host_Type.Server) { ret = AddServer(newclient); } else { Debug.Assert(false); } if(ret != null) {//either a pairing occured or a client connected if(ret.Count == 1) { if(OnClientConnectEvent != null) OnClientConnectEvent(ret.Select(a => a.ClientObject).ToList()); } else if(ret.Count == 2) { if(OnPairedEvent != null) OnClientConnectEvent(ret.Select(a => a.ClientObject).ToList()); } else Debug.Assert(false);//never hit this!! } return ret; }
public void PendingClientDisconnecting(ServerSocket s) { }