Beispiel #1
0
        private void RecieveCallback(IAsyncResult ar)
        {
            DataAsyncState state = ar.AsyncState as DataAsyncState;

            try {
                if (state.Client != null && state.Client.Connected)
                {
                    int packetSize;
                    packetSize = state.Client.EndReceive(ar);
                    if (packetSize < 1)
                    {
                        // Close the client connection since it disconnected.
                        DoDisconnect(state.Client);
                        return;
                    }
                    string stringData = encoding.GetString(state.Data, 0, packetSize);
                    dataCallback(this, new DataRecievedEventArgs(stringData, state.Client.RemoteEndPoint, state.Client, packetSize, state.Index));
                    DataAsyncState state2 = new DataAsyncState(bufferSize, state.Client, state.Index);
                    state.Client.BeginReceive(state2.Data, 0, bufferSize, SocketFlags.None,
                                              new AsyncCallback(RecieveCallback), state2);
                }
            } catch (SocketException sockex) {
                if ((SocketError)sockex.ErrorCode != SocketError.Success)
                {
                    RaiseError(sockex.Message, "", (SocketError)sockex.ErrorCode);
                    DoDisconnect(state.Client);
                }
            } catch (ObjectDisposedException) {
                DoDisconnect(state.Client);
                return;
            } catch {
                DoDisconnect(state.Client);
            }
        }
Beispiel #2
0
        private void ListenCallback(IAsyncResult ar)
        {
            try {
                System.Net.Sockets.Socket listener = ar.AsyncState as System.Net.Sockets.Socket;
                if (listener == null)
                {
                    //throw (new Exception("Listener Socket no longer exists"));
                    Exceptions.ErrorLogger.WriteToErrorLog(new Exception("Listener socket no longer exists"), "Listener socket no longer exists");
                }

                //CErrorLogger.WriteToErrorLog(new Exception("Listen callback hit"), "Listen callback hit");

                System.Net.Sockets.Socket client = listener.EndAccept(ar);

                if (listening != true)
                {
                    listener.Close();
                    return;
                }

                int index = FindOpenIndex();
                if (index == -1)
                {
                    Exceptions.ErrorLogger.WriteToErrorLog(new Exception("Server Full"), "Server Full");
                    client.Close();
                }
                else
                {
                    clients[index] = client;
                    connectionCallback(this, new ConnectionRecievedEventArgs(client.RemoteEndPoint, client, index));

                    DataAsyncState state = new DataAsyncState(bufferSize, client, index);
                    if (client.Connected)
                    {
                        client.BeginReceive(state.Data, 0, bufferSize, SocketFlags.None,
                                            new AsyncCallback(RecieveCallback), state);
                    }
                }
                listener.BeginAccept(new AsyncCallback(ListenCallback), listener);
            } catch (ObjectDisposedException ex) {
                Exceptions.ErrorLogger.WriteToErrorLog(ex, "Disposed exception, server listener.");
                serverSocket.BeginAccept(new AsyncCallback(ListenCallback), serverSocket);
                return;
            } catch (SocketException socketex) {
                Exceptions.ErrorLogger.WriteToErrorLog(socketex, "Socket exception, server listener.");
                RaiseError(socketex.Message, "", (SocketError)socketex.ErrorCode);
                serverSocket.BeginAccept(new AsyncCallback(ListenCallback), serverSocket);
            } catch (Exception ex) {
                Exceptions.ErrorLogger.WriteToErrorLog(ex, "Normal exception, server listener.");
                serverSocket.BeginAccept(new AsyncCallback(ListenCallback), serverSocket);
            }
        }
 private void RecieveCallback(IAsyncResult ar)
 {
     DataAsyncState state = ar.AsyncState as DataAsyncState;
     try {
         if (state.Client != null && state.Client.Connected) {
             int packetSize;
             packetSize = state.Client.EndReceive(ar);
             if (packetSize < 1) {
                 // Close the client connection since it disconnected.
                 DoDisconnect(state.Client);
                 return;
             }
             string stringData = encoding.GetString(state.Data, 0, packetSize);
             dataCallback(this, new DataRecievedEventArgs(stringData, state.Client.RemoteEndPoint, state.Client, packetSize, state.Index));
             DataAsyncState state2 = new DataAsyncState(bufferSize, state.Client, state.Index);
             state.Client.BeginReceive(state2.Data, 0, bufferSize, SocketFlags.None,
                               new AsyncCallback(RecieveCallback), state2);
         }
     } catch (SocketException sockex) {
         if ((SocketError)sockex.ErrorCode != SocketError.Success) {
             RaiseError(sockex.Message, "", (SocketError)sockex.ErrorCode);
             DoDisconnect(state.Client);
         }
     } catch (ObjectDisposedException) {
         DoDisconnect(state.Client);
         return;
     } catch {
         DoDisconnect(state.Client);
     }
 }
        private void ListenCallback(IAsyncResult ar)
        {
            try {
                System.Net.Sockets.Socket listener = ar.AsyncState as System.Net.Sockets.Socket;
                if (listener == null) {
                    //throw (new Exception("Listener Socket no longer exists"));
                    Exceptions.ErrorLogger.WriteToErrorLog(new Exception("Listener socket no longer exists"), "Listener socket no longer exists");
                }

                //CErrorLogger.WriteToErrorLog(new Exception("Listen callback hit"), "Listen callback hit");

                System.Net.Sockets.Socket client = listener.EndAccept(ar);

                if (listening != true) {
                    listener.Close();
                    return;
                }

                int index = FindOpenIndex();
                if (index == -1) {
                    Exceptions.ErrorLogger.WriteToErrorLog(new Exception("Server Full"), "Server Full");
                    client.Close();
                } else {
                    clients[index] = client;
                    connectionCallback(this, new ConnectionRecievedEventArgs(client.RemoteEndPoint, client, index));

                    DataAsyncState state = new DataAsyncState(bufferSize, client, index);
                    if (client.Connected) {
                        client.BeginReceive(state.Data, 0, bufferSize, SocketFlags.None,
                                      new AsyncCallback(RecieveCallback), state);
                    }
                }
                listener.BeginAccept(new AsyncCallback(ListenCallback), listener);
            } catch (ObjectDisposedException ex) {
                Exceptions.ErrorLogger.WriteToErrorLog(ex, "Disposed exception, server listener.");
                serverSocket.BeginAccept(new AsyncCallback(ListenCallback), serverSocket);
                return;
            } catch (SocketException socketex) {
                Exceptions.ErrorLogger.WriteToErrorLog(socketex, "Socket exception, server listener.");
                RaiseError(socketex.Message, "", (SocketError)socketex.ErrorCode);
                serverSocket.BeginAccept(new AsyncCallback(ListenCallback), serverSocket);
            } catch (Exception ex) {
                Exceptions.ErrorLogger.WriteToErrorLog(ex, "Normal exception, server listener.");
                serverSocket.BeginAccept(new AsyncCallback(ListenCallback), serverSocket);
            }
        }