private void OnDispose() { TcpServer.DisposeSocket(mSocket); try { if (Package != null) { Package.Channel = null; } mSocket = null; mSendSAEA.SetBuffer(null, 0, 0); mReceiveSAEA.SetBuffer(null, 0, 0); mSendSAEA.Completed -= OnSend; mReceiveSAEA.Completed -= OnReceive; while (mDatas.Count > 0) { SendData(GetSendData()); } if (ChannelDisposed != null) { ChannelDisposed(Server, new ChannelEventArgs { Channel = this }); } } finally { mProperties.Clear(); mServer = null; } }
private void SendData(IData data) { try { if (IsDisposed) { OnSendEvent(data, SendStatus.ChannelDisposed); return; } if (Package != null) { Package.Channel = null; } Buffer.BlockCopy(data.Array, 0, mSendSAEA.Buffer, 0, data.Count); mSendSAEA.SetBuffer(0, data.Count); mSendSAEA.UserToken = data; if (!mSocket.SendToAsync(mSendSAEA)) { OnSend(this, mSendSAEA); } } catch (Exception e_) { Sending = false; InvokeError(e_); OnSendEvent(data, SendStatus.Error); } }
private void OnSend(object sender, System.Net.Sockets.SocketAsyncEventArgs e) { if (e.SocketError == System.Net.Sockets.SocketError.Success && e.BytesTransferred > 0) { if (e.BytesTransferred != e.Count) { e.SetBuffer(e.Offset + e.BytesTransferred, e.Count - e.BytesTransferred); if (!Socket.SendAsync(e)) { OnSend(this, e); } } else { if (mSendUserToken.Count > 0) { SendData(mSendUserToken.Data); } else { Sending = false; AddSendData(null); OnSendEvent(mSendUserToken.Data, SendStatus.Success); } } } else { Sending = false; OnSendEvent(mSendUserToken.Data, SendStatus.Error); CloseStatus = "send error " + e.SocketError.ToString(); Dispose(); } }
// Assigns a buffer from the buffer pool to the // specified SocketAsyncEventArgs object // <returns>true if the buffer was successfully set, else false</returns> public bool SetBuffer(System.Net.Sockets.SocketAsyncEventArgs args) { if (freeIndexPool.Count > 0) { args.SetBuffer(buffer, freeIndexPool.Pop(), bufferSize); } else { if ((numBytes - bufferSize) < currentIndex) { return(false); } args.SetBuffer(buffer, currentIndex, bufferSize); currentIndex += bufferSize; } return(true); }
static int _m_SetBuffer(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); System.Net.Sockets.SocketAsyncEventArgs gen_to_be_invoked = (System.Net.Sockets.SocketAsyncEventArgs)translator.FastGetCSObj(L, 1); int gen_param_count = LuaAPI.lua_gettop(L); if (gen_param_count == 3 && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 2) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3)) { int _offset = LuaAPI.xlua_tointeger(L, 2); int _count = LuaAPI.xlua_tointeger(L, 3); gen_to_be_invoked.SetBuffer( _offset, _count); return(0); } if (gen_param_count == 4 && (LuaAPI.lua_isnil(L, 2) || LuaAPI.lua_type(L, 2) == LuaTypes.LUA_TSTRING) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 4)) { byte[] _buffer = LuaAPI.lua_tobytes(L, 2); int _offset = LuaAPI.xlua_tointeger(L, 3); int _count = LuaAPI.xlua_tointeger(L, 4); gen_to_be_invoked.SetBuffer( _buffer, _offset, _count); return(0); } } catch (System.Exception gen_e) { return(LuaAPI.luaL_error(L, "c# exception:" + gen_e)); } return(LuaAPI.luaL_error(L, "invalid arguments to System.Net.Sockets.SocketAsyncEventArgs.SetBuffer!")); }
private static void Sending_Completed(object sender, System.Net.Sockets.SocketAsyncEventArgs e) { var arg = new System.Net.Sockets.SocketAsyncEventArgs() { }; arg.Completed += Receiving_Completed; arg.SetBuffer(new byte[1000], 0, 1000); _socket.ReceiveAsync(arg); //throw new NotImplementedException(); }
private static void Sending_Completed(object sender, System.Net.Sockets.SocketAsyncEventArgs e) { var arg = new System.Net.Sockets.SocketAsyncEventArgs() { }; arg.Completed += Receiving_Completed; arg.SetBuffer(new byte[1000], 0, 1000); _socket.ReceiveAsync(arg); //throw new NotImplementedException(); }
public UdpChannel(System.Net.Sockets.Socket socket, IServer server, System.Net.EndPoint remotEndPoint) { mID = Guid.NewGuid().ToString("N"); mServer = server; mSocket = socket; mEndPoint = remotEndPoint; mSendSAEA = new System.Net.Sockets.SocketAsyncEventArgs(); mSendSAEA.SetBuffer(new byte[server.SendBufferSize], 0, server.SendBufferSize); mSendSAEA.RemoteEndPoint = mEndPoint; mSendSAEA.Completed += OnSend; Status = ChannelStatus.None; }
public Channel(TcpServer server, System.Net.Sockets.Socket socket) { mID = Guid.NewGuid().ToString("N"); mServer = server; mSendSAEA = new System.Net.Sockets.SocketAsyncEventArgs(); mReceiveSAEA = new System.Net.Sockets.SocketAsyncEventArgs(); mSendSAEA.SetBuffer(new byte[Server.SendBufferSize], 0, server.SendBufferSize); mReceiveSAEA.SetBuffer(new byte[server.ReceiveBufferSize], 0, server.ReceiveBufferSize); mSendSAEA.Completed += OnSend; mReceiveSAEA.Completed += OnReceive; mSocket = socket; mEndPoint = socket.RemoteEndPoint; Status = ChannelStatus.None; }
public static void Execute(IPEndPoint IPEndPoint, byte[] command) { _socket = new System.Net.Sockets.Socket( System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); var arg = new System.Net.Sockets.SocketAsyncEventArgs() { RemoteEndPoint = IPEndPoint }; arg.SetBuffer(command, 0, command.Length); arg.Completed += Sending_Completed; var result = _socket.ConnectAsync(arg); //socket.SendToAsync(arg); }
public static void Execute(IPEndPoint IPEndPoint, byte[] command) { _socket = new System.Net.Sockets.Socket( System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); var arg = new System.Net.Sockets.SocketAsyncEventArgs() { RemoteEndPoint = IPEndPoint }; arg.SetBuffer(command, 0, command.Length); arg.Completed += Sending_Completed; var result = _socket.ConnectAsync(arg); //socket.SendToAsync(arg); }
public uint Send(Byte[] buff, int len, SocketFlags flags) { uint AmountWrite = 0; if (mSocket != null) { mWriteDone.Reset(); System.Net.Sockets.SocketAsyncEventArgs socketEventArg = new System.Net.Sockets.SocketAsyncEventArgs(); socketEventArg.RemoteEndPoint = mSocket.RemoteEndPoint; socketEventArg.UserToken = this; socketEventArg.Completed += new EventHandler <System.Net.Sockets.SocketAsyncEventArgs>( delegate(object s, System.Net.Sockets.SocketAsyncEventArgs e) { if (e.SocketError == System.Net.Sockets.SocketError.Success) { Socket userToken = e.UserToken as Socket; userToken.mWriteAmount = (uint)e.BytesTransferred; userToken.mWriteDone.Set(); } //else //{ // throw new SocketException((int)(e.SocketError)); //} }); // Add the data to be sent into the buffer socketEventArg.SetBuffer(buff, 0, len); mSocket.SendAsync(socketEventArg); mWriteDone.WaitOne(); AmountWrite = mWriteAmount; } return(AmountWrite); }
// Removes the buffer from a SocketAsyncEventArg object. // This frees the buffer back to the buffer pool public void FreeBuffer(System.Net.Sockets.SocketAsyncEventArgs args) { freeIndexPool.Push(args.Offset); args.SetBuffer(null, 0, 0); }
public void Connect() { var connArgs = new System.Net.Sockets.SocketAsyncEventArgs(); Action <Object, System.Net.Sockets.SocketAsyncEventArgs> connCompleted = (object _, System.Net.Sockets.SocketAsyncEventArgs __) => { if (connArgs.SocketError != System.Net.Sockets.SocketError.Success) { Console.WriteLine("Error Connecting"); throw new Exception(connArgs.SocketError.ToString()); } byte[] buffer = new byte[4096 * 4096]; StringBuilder sb = new StringBuilder(""); var nested = 0; Action <Object, System.Net.Sockets.SocketAsyncEventArgs> action = null; Action <Object, System.Net.Sockets.SocketAsyncEventArgs> rcvCompleted = (object ___, System.Net.Sockets.SocketAsyncEventArgs args) => { if (args.BytesTransferred <= 0) { return; } if (args.SocketError != System.Net.Sockets.SocketError.Success) { Console.WriteLine("Error Receiving"); throw new Exception(args.SocketError.ToString()); } string content = Encoding.UTF8.GetString(buffer, 0, args.BytesTransferred); foreach (char c in content) { sb.Append(c); if (c == '{') { nested++; } if (c == '}') { nested--; } if (nested < 0) { Console.WriteLine("negative nesting!"); throw new Exception("negative nesting"); } if (nested == 0) { string str = sb.ToString(); sb.Clear(); // Console.WriteLine("unwrapped nesting!"); // Console.WriteLine(str); var obj = JObject.Parse(str); Call(obj["evt"].ToString(), obj["data"] as JObject); } } args.SetBuffer(buffer, 0, buffer.Length); if (!socket.ReceiveAsync(args)) { action.Invoke(null, args); } }; action = rcvCompleted; var rcvArgs = new System.Net.Sockets.SocketAsyncEventArgs(); rcvArgs.SetBuffer(buffer, 0, buffer.Length); rcvArgs.Completed += new EventHandler <System.Net.Sockets.SocketAsyncEventArgs>( rcvCompleted ); if (!socket.ReceiveAsync(rcvArgs)) { rcvCompleted(null, rcvArgs); } Call("connect", null); }; connArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(host), port); connArgs.Completed += new EventHandler <System.Net.Sockets.SocketAsyncEventArgs>( connCompleted ); if (!socket.ConnectAsync(connArgs)) { connCompleted(null, connArgs); } }
public uint Receive(Byte[] buff, int len, SocketFlags flags) { if (mForceReconnect) { mReadDone.Reset(); return(SOCKET_ERROR); } uint AmountRead = 0; if (mSocket != null) { if (mAvailable != 0) { AmountRead = (mAvailable <= len) ? mAvailable : (uint)len; if (mOffset + AmountRead > MAX_BUFFER_SIZE) { throw new SocketException(); } for (int i = 0; i < AmountRead; ++i) { buff[i] = mReadBuffer[mOffset + i]; } mAvailable -= AmountRead; mOffset += AmountRead; if (mAvailable == 0) { Array.Clear(mReadBuffer, 0, MAX_BUFFER_SIZE); mOffset = 0; } } else { mReadDone.WaitOne(); mReadDone.Reset(); } if (mReadAmount != 0) { if (mReadAmount <= (uint)len) { AmountRead = mReadAmount; mAvailable = 0; mOffset = 0; } else { AmountRead = (uint)len; mAvailable = mReadAmount - (uint)len; mOffset = (uint)len; } if (AmountRead > MAX_BUFFER_SIZE) { throw new SocketException(); } for (int i = 0; i < AmountRead; ++i) { buff[i] = mReadBuffer[i]; } if (mAvailable == 0) { Array.Clear(mReadBuffer, 0, MAX_BUFFER_SIZE); } mReadAmount = 0; } if (mAvailable == 0) { System.Net.Sockets.SocketAsyncEventArgs socketEventArg = new System.Net.Sockets.SocketAsyncEventArgs(); socketEventArg.RemoteEndPoint = mSocket.RemoteEndPoint; socketEventArg.UserToken = this; // Setup the buffer to receive the data socketEventArg.SetBuffer(mReadBuffer, 0, MAX_BUFFER_SIZE); socketEventArg.Completed += new EventHandler <System.Net.Sockets.SocketAsyncEventArgs>( delegate(object s, System.Net.Sockets.SocketAsyncEventArgs e) { Socket userToken = e.UserToken as Socket; if (null != userToken) { if (e.SocketError == System.Net.Sockets.SocketError.Success) { userToken.mReadAmount = (uint)e.BytesTransferred; userToken.mReadDone.Set(); } else { mForceReconnect = true; userToken.mReadDone.Set(); } if (!userToken.Connected) { mForceReconnect = true; userToken.mReadDone.Set(); } } }); mSocket.ReceiveAsync(socketEventArg); } } if (!Connected) { return(SOCKET_ERROR); } return(AmountRead); }
/// <summary> /// Send datas. /// </summary> /// <param name="data">Datas to send.</param> public void Send(string data) { System.Net.Sockets.SocketAsyncEventArgs args = new System.Net.Sockets.SocketAsyncEventArgs(); if (p_twoSensesSocket.Connected) { receiveDone.Set(); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data + String.Empty); // p_socketSendEventArgs.SetBuffer(buffer, 0, buffer.Length); args.Completed += new EventHandler<System.Net.Sockets.SocketAsyncEventArgs>(SocketSendEventArgs_Completed); args.SetBuffer(buffer, 0, buffer.Length); args.RemoteEndPoint = endPoint; p_twoSensesSocket.ConnectAsync(args); receiveDone.Reset(); } }