Example #1
0
 /// <summary>
 /// aƱadir un SocketState
 /// </summary>
 /// <param name="__ss"></param>
 public void BeginReceive(Route route)
 {
     // empezamos a recibir mensajes
     route.BeginReceive(route.bBufferIn, 0,
                        route.bBufferIn.Length - 1,
                        new AsyncCallback(ContinuationReceiveCallback), route);
 }
Example #2
0
        internal void ContinuationReceiveCallback(IAsyncResult iar)
        {
            Route route = null;

            try
            {
                // cogemos el objeto socketstate
                route = (Route)iar.AsyncState;

                if (route != null && route.Connected)
                {
                    // recogemos los bytes del buffer a leer
                    int numBytesReceived = route.EndReceive(iar);
                    // variable global que almacena el numero de bytes totales recibidos
                    bytesReceived += (uint)numBytesReceived;
                    // marcamos la fecha/hora de la ultima
                    route.UpdateActivity();
                    // comprobamos el numero de bytes recibidos
                    if (numBytesReceived > 0)
                    {
                        route.BufferStream.Write(route.bBufferIn, 0, numBytesReceived);

                        // volvemos a leer
                        route.BeginReceive(route.bBufferIn, 0,
                                           route.bBufferIn.Length,
                                           new AsyncCallback(ContinuationReceiveCallback), route);
                    }
                    else
                    {
                        Node.LogAppendLine("the other end of the socket has closed gracefully {0}", route.RemoteEndPoint);
                        route.Failed = true;
                    }
                }
            }
            catch (BadProtocolException)
            {
                route.Failed = true;
                Node.LogAppendLine("Bad Protocol Exception");
            }
            catch (ObjectDisposedException err)
            {
                route.Failed = true;
                Node.LogAppendLine("EndReceive closing disposed socket {0} {1}", err.Message, err.ObjectName);
            }
            catch (SocketException err)
            {
                route.Failed = true;
                Node.LogAppendLine("Error en el socket: {0}\n{1}", err.ErrorCode, err.Message);
            }
            catch (Exception err)
            {
                route.Failed = true;
                Node.LogAppendLine(err);
            }
            finally
            {
                if (route.Failed)
                {
                    localNode.RouteTable.RemoveRoute(route);
                }
            }
        }