public void Send( NetBitStream bts ) { if(!_socket.Connected) { return; } //创建NetworkStream NetworkStream ns; //加锁,避免多线程下出问题 lock(_socket) { ns = new NetworkStream( _socket ); } if ( ns.CanWrite ) { try { ns.BeginWrite( bts.BYTES, 0, bts.Length, new System.AsyncCallback( SendCallback ), ns ); } catch(System.Exception) { PushPacket((ushort)MessageIdentifiers.ID.CONNCET_LOST,""); Disconnect(0); } } }
// 接收消息体 void ReceiveBody(System.IAsyncResult ar) { NetBitStream stream = (NetBitStream)ar.AsyncState; try { int read = _socket.EndReceive(ar); // 用户已下线 if (read < 1) { Disconnect(0); PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, ""); return; } PushPacket2(stream); // 下一个读取 _socket.BeginReceive(stream.BYTES, 0, NetBitStream.header_length, SocketFlags.None, new System.AsyncCallback(ReceiveHeader), stream); } catch (System.Exception e) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, e.Message); Disconnect(0); } }
// 发送消息 public void Send(NetBitStream bts) { if (!_socket.Connected) { return; } NetworkStream ns; lock (_socket) { ns = new NetworkStream(_socket); } if (ns.CanWrite) { try { ns.BeginWrite(bts.BYTES, 0, bts.Length, new System.AsyncCallback(SendCallback), ns); } catch (System.Exception) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, ""); Disconnect(0); } } }
void ConnectionCallback(System.IAsyncResult ar) { NetBitStream stream = new NetBitStream(); stream._socket = (Socket)ar.AsyncState; try { _socket.EndConnect(ar); _socket.SendTimeout = _sendTimeout; _socket.ReceiveTimeout = _revTimeout; PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_REQUEST_ACCEPTED, ""); _socket.BeginReceive(stream.BYTES, 0, NetBitStream.header_length, SocketFlags.None, new System.AsyncCallback(ReceiveHeader), stream); } catch (System.Exception e) { if (e.GetType() == typeof(SocketException)) { if (((SocketException)e).SocketErrorCode == SocketError.ConnectionRefused) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_ATTEMPT_FAILED, e.Message); } else { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, e.Message); } } Disconnect(0); } }
// 接收头消息 void ReceiveHeader(System.IAsyncResult ar) { NetBitStream stream = (NetBitStream)ar.AsyncState; try { int read = stream._socket.EndReceive(ar); // 服务器断开连接 if (read < 1) { // 发送消息 失去一个连接 PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, "", stream._socket); return; } // 获得消息体长度 stream.DecodeHeader(); // 下一个读取 stream._socket.BeginReceive(stream.BYTES, NetBitStream.header_length, stream.BodyLength, SocketFlags.None, new System.AsyncCallback(ReceiveBody), stream); } catch (System.Exception e) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, e.Message, stream._socket); } }
// 接受一个新的连接 void ListenTcpClient(System.IAsyncResult ar) { // bit stream NetBitStream stream = new NetBitStream(); try { // 取得服务器端的Socket // Socket listener = (Socket)ar.AsyncState; // 取得客户端的socket Socket client = _listener.EndAccept(ar); stream._socket = client; // 设置 timeout时间 client.SendTimeout = _sendTimeout; client.ReceiveTimeout = _revTimeout; // 接收从服务器返回的头信息 client.BeginReceive(stream.BYTES, 0, NetBitStream.header_length, SocketFlags.None, new System.AsyncCallback(ReceiveHeader), stream); // 发送消息 建立一个新的连接 PushPacket((ushort)MessageIdentifiers.ID.NEW_INCOMING_CONNECTION, "", client); } catch (System.Exception) { //出现错误 } // 继续接受其它连接 _listener.BeginAccept(new System.AsyncCallback(ListenTcpClient), _listener); }
void PushPacket2(NetBitStream stream) { NetPacket packet = new NetPacket(); stream.BYTES.CopyTo(packet._bytes, 0); packet._peer = stream._socket; _netMgr.AddPacket(packet); }
public void Send(NetBitStream bts, Socket peer) { NetworkStream ns; lock (peer) { ns = new NetworkStream(peer); } if (ns.CanWrite) { try { ns.BeginWrite(bts.BYTES, 0, bts.Length, new System.AsyncCallback(SendCallback), ns); } catch (System.Exception) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, "", peer); } } }
//发送数据 public void Send( NetBitStream bts, Socket peer ) { NetworkStream ns; lock ( peer ) { ns = new NetworkStream( peer ); } if ( ns.CanWrite ) { try { ns.BeginWrite( bts.BYTES, 0, bts.Length, new System.AsyncCallback( SendCallback ), ns ); } catch (System.Exception) { PushPacket( (ushort)MessageIdentifiers.ID.CONNCET_LOST, "", peer ); } } }
void ListenTcpClient(System.IAsyncResult ar) { NetBitStream stream = new NetBitStream(); try { Socket client = _listener.EndAccept(ar); stream._socket = client; client.SendTimeout = _sendTimeout; client.ReceiveTimeout = _revTimeout; client.BeginReceive(stream.BYTES, 0, NetBitStream.header_length, SocketFlags.None, new System.AsyncCallback(ReceiveHeader), stream); PushPacket((ushort)MessageIdentifiers.ID.NEW_INCOMING_CONNECTION, "", client); } catch (System.Exception) { } _listener.BeginAccept(new System.AsyncCallback(ListenTcpClient), _listener); }
//向Network Manager的队列传递NetBitStream对象的数据 void PushPacket2(NetBitStream stream) { NetPacket packet = new NetPacket(); stream.BYTES.CopyTo( packet._bytes, 0 ); packet._peer = stream._socket; _netMgr.AddPacket( packet ); }
void ConnectionCallback(System.IAsyncResult ar) { //创建NetBitStream对象 NetBitStream stream = new NetBitStream(); //获得服务器socket stream._socket = (Socket)ar.AsyncState; try { //与服务器取得连接,如果连接失败,这里将抛出异常 _socket.EndConnect( ar ); //设置发送和接受的超时事件 _socket.SendTimeout = _sendTimeout; _socket.ReceiveTimeout = _recvTimeout; //向逻辑处理队列发送成功连接的消息 PushPacket( (ushort)MessageIdentifiers.ID.CONNECTION_REQUEST_ACCEPTED, "" ); //开始接受从服务器发来的数据 _socket.BeginReceive( stream.BYTES, 0, NetBitStream.header_length, SocketFlags.None, new System.AsyncCallback( ReceiveHeader ), stream ); } catch(System.Exception e) { //出现异常,错误处理 if ( e.GetType() == typeof( SocketException ) ) { if ( ( (SocketException)e ).SocketErrorCode == SocketError.ConnectionRefused ) { PushPacket( (ushort)MessageIdentifiers.ID.CONNECTION_ATTEMPT_FAILED, e.Message ); } else { PushPacket( (ushort)MessageIdentifiers.ID.CONNCET_LOST, e.Message ); } } Disconnect( 0 ); } }
// 从数据流中拷贝数据 public void CopyBytes(NetBitStream stream) { stream.BYTES.CopyTo(_bytes, 0); _length = stream.Length; }
void ListenTcpClient(System.IAsyncResult ar) { //创建一个NetBitStream对象保存接受到的数据 NetBitStream stream = new NetBitStream(); try { //取得客户端的socket Socket client = _listener.EndAccept( ar ); stream._socket = client; //设置发送接受超时 client.SendTimeout = _sendTimeout; client.ReceiveTimeout = _recvTimeout; //接受从服务器返回的头信息 client.BeginReceive( stream.BYTES, 0, NetBitStream.header_length, SocketFlags.None, new System.AsyncCallback( ReceiveHeader ), stream ); //向逻辑处理队列发送新连接的消息 PushPacket( (ushort)MessageIdentifiers.ID.NEW_INCOMING_CONNECTION, "", client ); } catch(System.Exception) { //出现错误 } //继续接受其他连接 _listener.BeginAccept( new System.AsyncCallback( ListenTcpClient ), _listener ); }